package cn.org.mrliu.ssm.util.upload;

import java.util.Arrays;

public class UploadFileValidate {
	/**
	 * .b().kb().mb().gb()
	 * 
	 * UploadFileValidate.contentType("image/png","image/jpeg")
	 * 					 .extension("jpg","jpeg","png")
	 * 					 .size(1)
	 * 					 .mb()					 
	 * 					 .totalSize(10)
	 * 					 .contentTypeError("请上传图片类型")
	 * 					 .extensionError("请上传.jpg,.jpeg,.png后缀的文件")
	 * 					 .sizeError("单个文件最大_size_")
	 * 					 .totalSizeErrorMessage(所有文件最大_size_")
	 * ;
	 */
	public static final String UNIT_B = "B";
	public static final String UNIT_KB = "KB";
	public static final String UNIT_MB = "MB";
	public static final String UNIT_GB = "GB";

	
	private String[] contentTypes;
	private String[] extensions;
	private Long size;
	private Long sizeB;
	private Long totalSize;
	private Long totalSizeB;
	private String unit;
	private String contentTypeError;
	private String extensionError;
	private String sizeError;
	private String totalSizeError;
	
	
	public static UploadFileValidate build() {
		UploadFileValidate uploadFileValidate = new UploadFileValidate();
		return uploadFileValidate;
	}
	
	public UploadFileValidate contentTypes(String...contentType) {
		this.contentTypes = contentType;
		return this;
	}
	
	public UploadFileValidate extensions(String...extension) {
		this.extensions = extension;
		return this;
	}

	public UploadFileValidate size(Long size) {
		this.size = size;
		return this;
	}
	
	public UploadFileValidate contentTypeError(String contentTypeError) {
		this.contentTypeError = contentTypeError;
		return this;
	}
	
	
	public UploadFileValidate extensionError(String extensionError) {
		this.extensionError = extensionError;
		return this;
	}
	
	public UploadFileValidate sizeError(String sizeError) {
		this.sizeError = sizeError;
		return this;
	}
	
	public UploadFileValidate totalSizeError(String totalSizeError) {
		this.totalSizeError = totalSizeError;
		return this;
	}
	
	public UploadFileValidate b() {
		this.unit = UNIT_B;
		return this;
	}
	
	public UploadFileValidate kb() {
		this.unit = UNIT_KB;
		return this;
	}
	
	public UploadFileValidate mb() {
		this.unit = UNIT_MB;
		return this;
	}
	
	public UploadFileValidate gb() {
		this.unit = UNIT_GB;
		return this;
	}

	
	
	
	
	
	// ------------ getter/setter
	public String[] getContentTypes() {
		return contentTypes;
	}

	public void setContentTypes(String[] contentTypes) {
		this.contentTypes = contentTypes;
	}

	public String[] getExtensions() {
		return extensions;
	}

	public void setExtensions(String[] extensions) {
		this.extensions = extensions;
	}

	public Long getSize() {
		return size;
	}

	public void setSize(Long size) {
		this.size = size;
	}

	public Long getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(Long totalSize) {
		this.totalSize = totalSize;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		if (unit == null) {
			this.unit = UNIT_KB;
		}
		unit = unit.trim();
		if (unit.equals(UNIT_B)
				||unit.equals(UNIT_KB)
				||unit.equals(UNIT_MB)
				||unit.equals(UNIT_GB)) {
			this.unit = unit;
		}
	}

	public String getContentTypeError() {
		return contentTypeError;
	}

	public void setContentTypeError(String contentTypeError) {
		this.contentTypeError = contentTypeError;
	}

	public String getExtensionError() {
		return extensionError;
	}

	public void setExtensionError(String extensionError) {
		this.extensionError = extensionError;
	}

	public String getSizeError() {
		return sizeError;
	}

	public void setSizeError(String sizeError) {
		this.sizeError = sizeError;
	}

	public String getTotalSizeError() {
		return totalSizeError;
	}

	public void setTotalSizeError(String totalSizeError) {
		this.totalSizeError = totalSizeError;
	}

	public Long getSizeB() {
		if (size!=null&&size>0) {
			if (unit.equals(UNIT_B)) {
				this.sizeB = this.size;
			} else if (unit.equals(UNIT_KB)) {
				this.sizeB = this.size * 1024;
			}else if (unit.equals(UNIT_MB)) {
				this.sizeB = this.size * 1024 * 1024;
			}else if (unit.equals(UNIT_GB)) {
				this.sizeB = this.size * 1024 * 1024 * 1024;
			}
		}
		return sizeB;
	}

	public void setSizeB(Long sizeB) {
		this.sizeB = sizeB;
	}

	public Long getTotalSizeB() {
		if (totalSize!=null&&totalSize>0) {
			if (unit.equals(UNIT_B)) {
				this.totalSizeB = this.totalSize;
			} else if (unit.equals(UNIT_KB)) {
				this.totalSizeB = this.totalSize * 1024;
			}else if (unit.equals(UNIT_MB)) {
				this.totalSizeB = this.totalSize * 1024 * 1024;
			}else if (unit.equals(UNIT_GB)) {
				this.totalSizeB = this.totalSize * 1024 * 1024 * 1024;
			}
		}
		return this.totalSizeB;
	}

	public void setTotalSizeB(Long totalSizeB) {
		this.totalSizeB = totalSizeB;
	}

	@Override
	public String toString() {
		return "UploadFileValidate [contentTypes=" + Arrays.toString(contentTypes) + ", extensions="
				+ Arrays.toString(extensions) + ", size=" + size + ", totalSize=" + totalSize + ", unit=" + unit
				+ ", contentTypeError=" + contentTypeError + ", extensionError=" + extensionError + ", sizeError="
				+ sizeError + ", totalSizeError=" + totalSizeError + "]";
	}
	
}
